<?php
declare(strict_types=1);
namespace app\common\service\msg;

use app\common\model\MpSubscribe;
use app\common\model\Third;
use app\common\service\MsgService;
use app\common\model\Msg;

class WechatMsg extends MsgService{

    protected $msg_type='wechat';
    
    const 公司入驻审核='BoNf1XXTVK-CEI60Lhm1B8tp_ErlT85LMa4mMhyalig';
    const 加急配送提醒="Xt1JNb9UhWXmiFYyBuxJvKf2qyRGioSSHUogMdESTXc";
    const 支付成功通知="-SA2WYDHEmOOUKEXwnMFJDhwul108Vk1G2LS7_n8m_0";
    const 公司审核成功='ObScYlbUut7rfSW5hPFlUz3SAqflYJxSHUUURvAyfCQ';
    const 月付支付通知='aiJO5DO90zgDXsSF8GOHxORFRV67Zajgk0EOa-8pfw4';
    const 客户采购订单提醒='oNGC5by7yuBrtOIB5FARPFhgBe3yO00NJ5Bulfm0CK0';
    const 管理员添加新公司='y6H1Eo7oFLqN8LqJC6q6ZH99gGQaM_0VabJ54Ue2F1U';


    protected function sendEvent(Msg $msg): bool
    {
        $config=[
            'appid'=>site_config("addons.uniapp_mpapp_id"),
            'appsecret'=>site_config("addons.uniapp_mpapp_secret"),
        ];
        // 实例接口
        $wechat = new \WeChat\Template($config);
        // 执行操作
        try{
            $wechat->send(json_decode($msg->content,true));
            return true;
        }catch (\Exception $e){
            $this->error=$e->getMessage();
            return false;
        }
    }

    /**
     * 新店铺入驻审核
     */
    public static function newCustomerApply($user_id,$register_id,$applyinfo)
    {
        $openid=self::getUserMpOpenid($user_id);
        if(!$openid){
            return;
        }
        $name='';
        if($applyinfo['customer_type']=='company'){
            $name='【公司】'.$applyinfo['name'];
        }
        if($applyinfo['customer_type']=='family'){
            $name='【住户】'.$applyinfo['address'];
        }
        $postdata=[
            'touser'=>$openid,
            'template_id'=>self::公司入驻审核,
            'miniprogram'=>[
                'appid'=>site_config("addons.uniapp_miniapp_id"),
                'pagepath'=>'/pages/admin/addstore?register_id='.$register_id,
            ],
            'data'=>[
                'thing1'=>['value'=>$name],
                'thing2'=>['value'=>$applyinfo['contact']],
                'phone_number3'=>['value'=>$applyinfo['mobile']]
            ]
        ];
        $postdata=json_encode($postdata,JSON_UNESCAPED_SLASHES);
        $service=self::newInstance();
        $service->create($postdata,$user_id);
    }

    public static function newXunjiaOrder($order,$user_id)
    {
        $openid=self::getUserMpOpenid($user_id);
        if(!$openid){
            return;
        }
        $customer_type=$order->customer->customer_type;
        $name='';
        if($customer_type=='company'){
            $name='【公司】'.$order->customer->name;
        }
        if($customer_type=='family'){
            $name='【住户】'.$order->customer->address;
        }
        $postdata=[
            'touser'=>$openid,
            'template_id'=>self::客户采购订单提醒,
            'miniprogram'=>[
                'appid'=>site_config("addons.uniapp_miniapp_id"),
                'pagepath'=>'/pages/admin/orderlist'
            ],
            'data'=>[
                'phone_number12'=>['value'=>$order->customer->mobile],
                'thing3'=>['value'=>$name],
                'character_string10'=>['value'=>$order->order_no],
                'thing5'=>['value'=>$order->goods->name.'，'.$order->spec->name.'，'.$order->number.$order->spec->unit],
            ]
        ];
        $postdata=json_encode($postdata,JSON_UNESCAPED_UNICODE);
        $service=self::newInstance();
        $service->create($postdata,$user_id);
    }

    public static function noticePayMonthBill($user_id,$bill)
    {
        $openid=self::getUserMpOpenid($user_id);
        if(!$openid){
            return;
        }
        $bill->starttime=substr($bill->starttime,0,10);
        $bill->endtime=substr($bill->endtime,0,10);
        $postdata=[
            'touser'=>$openid,
            'template_id'=>self::月付支付通知,
            'miniprogram'=>[
                'appid'=>site_config("addons.uniapp_miniapp_id"),
                'pagepath'=>'/pages/buy/yuefu?goods_id='.$bill->goods_id,
            ],
            'data'=>[
                'amount1'=>['value'=>$bill->total_money],
                'time2'=>['value'=>$bill->starttime.'~'.$bill->endtime],
                'thing8'=>['value'=>$bill->goods->name],
            ]
        ];
        $postdata=json_encode($postdata,JSON_UNESCAPED_UNICODE);
        $service=self::newInstance();
        $service->create($postdata,$user_id);
    }

    public static function hurryOrderDelivery($user_id,$order)
    {
        $openid=self::getUserMpOpenid($user_id);
        if(!$openid){
            return;
        }
        $postdata=[
            'touser'=>$openid,
            'template_id'=>self::加急配送提醒,
            'miniprogram'=>[
                'appid'=>site_config("addons.uniapp_miniapp_id"),
                'pagepath'=>'/pages/driver/today',
            ],
            'data'=>[
                'thing1'=>['value'=>'1笔订单需立即配送'],
                'thing2'=>['value'=>$order->number.$order->spec->unit.$order->goods->name],
                'phrase4'=>['value'=>$order->customer->contact],
            ]
        ];
        $postdata=json_encode($postdata,JSON_UNESCAPED_UNICODE);
        $service=self::newInstance();
        $service->create($postdata,$user_id);
    }

    public static function payOrderSuccess($order)
    {
        $openid=self::getUserMpOpenid($order->user_id);
        if(!$openid){
            return;
        }
        $postdata=[
            'touser'=>$openid,
            'template_id'=>self::支付成功通知,
            //'url'=>request()->domain().'/h5/#/pages/buy/index?name=order&goods_id='.$order->goods_id,
            'miniprogram'=>[
                'appid'=>site_config("addons.uniapp_miniapp_id"),
                'pagepath'=>'/pages/buy/index?name=order&goods_id='.$order->goods_id,
            ],
            'data'=>[
                'amount2'=>['value'=>$order->total_money],
                'time3'=>['value'=>$order->createtime],
                'character_string6'=>['value'=>$order->order_no],
                'thing8'=>['value'=>$order->number.$order->spec->unit.$order->goods->name],
            ]
        ];
        $postdata=json_encode($postdata,JSON_UNESCAPED_UNICODE);
        $service=self::newInstance();
        $service->create($postdata,$order->user_id);
    }

    public static function customerApplySuccess($customer)
    {
        $openid=self::getUserMpOpenid($customer->user_id);
        if(!$openid){
            return;
        }
        $name='';
        if($customer['customer_type']=='company'){
            $name='【公司】'.$customer['name'];
        }
        if($customer['customer_type']=='family'){
            $name='【住户】'.$customer['address'];
        }
        $postdata=[
            'touser'=>$openid,
            'template_id'=>self::公司审核成功,
            'miniprogram'=>[
                'appid'=>site_config("addons.uniapp_miniapp_id"),
                'pagepath'=>'/pages/index/index',
            ],
            'data'=>[
                'thing1'=>['value'=>$name],
                'thing2'=>['value'=>$customer['contact']],
                'phone_number3'=>['value'=>$customer['mobile']],
                'time4'=>['value'=>$customer['updatetime']],
            ]
        ];
        $postdata=json_encode($postdata,JSON_UNESCAPED_UNICODE);
        $service=self::newInstance();
        $service->create($postdata,$customer->user_id);
    }

    public static function addCustomerNoticeToUser($customer)
    {
        $openid=self::getUserMpOpenid($customer->user_id);
        if(!$openid){
            return;
        }
        $name='';
        if($customer['customer_type']=='company'){
            $name='【公司】'.$customer['name'];
        }
        if($customer['customer_type']=='family'){
            $name='【住户】'.$customer['address'];
        }
        $postdata=[
            'touser'=>$openid,
            'template_id'=>self::管理员添加新公司,
            'miniprogram'=>[
                'appid'=>site_config("addons.uniapp_miniapp_id"),
                'pagepath'=>'/pages/index/index',
            ],
            'data'=>[
                'thing2'=>['value'=>$name],
                'phone_number1'=>['value'=>$customer['mobile']],
                'time3'=>['value'=>$customer['createtime']],
            ]
        ];
        $postdata=json_encode($postdata,JSON_UNESCAPED_UNICODE);
        $service=self::newInstance();
        $service->create($postdata,$customer->user_id);
    }

    private static function getUserMpOpenid($user_id)
    {
        $unionid=Third::where(['user_id'=>$user_id])->value('unionid');
        $openid=MpSubscribe::where(['unionid'=>$unionid])->value('openid');
        return $openid;
    }
}